package animator.phantom.renderer.threedee;

import javax.media.j3d.Shape3D;
import javax.media.j3d.Appearance;
import com.sun.j3d.utils.geometry.NormalGenerator;
import com.sun.j3d.utils.geometry.GeometryInfo;
import javax.vecmath.Point3f;
import java.awt.Dimension;

public class TexturedRect3D extends Shape3D 
{
	public TexturedRect3D( Appearance app, Dimension size )
	{
		// --- Create geometry. Make longer size length 1.0f.
		float h, w;
		if(  size.height > size.width )
		{
			h = 1.0f;
			w = ((float) size.width) / ((float) size.height);
		}
		else
		{
			h = ((float) size.height) / ((float) size.width);
			w = 1.0f;
		}
		
		Point3f[] pts = new Point3f[4];
		pts[0] = new Point3f(w / 2.0f, -h / 2.0f, 0.0f);
		pts[1] = new Point3f(w / 2.0f, h / 2.0f, 0.0f);
		pts[2] = new Point3f(-w / 2.0f, h / 2.0f, 0.0f);
		pts[3] = new Point3f(-w / 2.0f, -h / 2.0f, 0.0f);

		int[] stripCounts= new int[1];
		stripCounts[0] = 4;
		int[] contourCount=new int[1];
		contourCount[0] = 1;

		GeometryInfo gInf = new GeometryInfo( GeometryInfo.POLYGON_ARRAY );
		gInf.setCoordinates( pts );
		gInf.setStripCounts( stripCounts );
		gInf.setContourCounts( contourCount );

		NormalGenerator ng = new NormalGenerator();
		ng.setCreaseAngle ((float) Math.toRadians(30) );// this may need to be configurable??
		ng.generateNormals( gInf );

		this.setGeometry( gInf.getGeometryArray() );

		//--- Set appeareance
		this.setAppearance( app );
	}

}//end class